package dang.action.order;

import java.util.List;

import net.sf.json.JSONObject;
import dang.action.DangAction;
import dang.dao.DaoFactory;
import dang.dao.OrderDao;
import dang.entity.ReceiveAddress;
import dang.entity.User;
import dang.exception.DangException;

public class AddressInfoAction extends DangAction{
	private List<ReceiveAddress> addresses;
	private ReceiveAddress address;
	public String execute() {
		User user=(User) session.get("user");
		int id = user.getId();
		OrderDao dao = DaoFactory.getOrderDao();
		try {
			addresses = dao.findByUserId(id);
		} catch (DangException e) {
			e.printStackTrace();
		}
		return "success";
	}
	
	public void getSelectedAddress() throws Exception{
		//TODO再次访问同一个action时，addresses为null
		User user=(User) session.get("user");
		int id = user.getId();
		OrderDao dao = DaoFactory.getOrderDao();
		addresses = dao.findByUserId(id);
		for (ReceiveAddress addr : addresses) {
			if(addr.getId()==address.getId()){
				address=addr;
				break;
			}
		}
		JSONObject json = JSONObject.fromObject(address);
		response.getWriter().println(json.toString());
	}
	
	public List<ReceiveAddress> getAddresses() {
		return addresses;
	}

	public void setAddresses(List<ReceiveAddress> addresses) {
		this.addresses = addresses;
	}

	public ReceiveAddress getAddress() {
		return address;
	}

	public void setAddress(ReceiveAddress address) {
		this.address = address;
	}

	
}
